package com.songyuesheng.app.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songyuesheng.app.commom.ContextHolder;
import com.songyuesheng.app.commom.Result;
import com.songyuesheng.app.entity.History;
import com.songyuesheng.app.mapper.HistoryMapper;
import com.songyuesheng.app.service.HistoryService;
import com.songyuesheng.app.service.OssUploaderService;
import com.songyuesheng.app.vo.HistoryVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author song
 * @Date 2025/3/28 11:04
 */
@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {

    @Resource
    private HistoryMapper historyMapper;

    @Resource
    private OssUploaderService ossUploaderService;

    @Override
    public Result<?> getHistory() {
        Long userId = ContextHolder.getUserId();
        LambdaQueryWrapper<History> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(History::getUserId, userId);
        List<History> histories = historyMapper.selectList(lambdaQueryWrapper);
        
        // 为每个历史记录生成OSS访问URL并转换为VO对象
        List<HistoryVo> result = histories.stream().map(history -> {
            String url = ossUploaderService.generatePresignedUrl(history.getObjectKey());
            return new HistoryVo(
                history.getRipeness(),  // 从History获取成熟度
                history.getDetectTime(), // 从History获取检测时间
                url                     // 生成的OSS访问URL
            );
        }).collect(Collectors.toList());
        
        return Result.success(result);
    }
}
